package com.sunny.ynhz.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.NewsStaticAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.NewsBean;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/1/8.
 */

public class NewsStaticActivity extends AppBaseTranslationTitleBarActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    List<NewsBean.DataBean.ContentBean> newsData = new ArrayList<>();
    private NewsStaticAdapter newsStaticAdapter;
    private RecyclerView refreshableView;
    private SwipeRefreshLayout swipeRefreshLayout_ly;
    int current = 1;
    private int id;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_static_activity);
        title = getIntent().getStringExtra("title");
        setTitle(title);
        id = getIntent().getIntExtra("id", -1);
        refreshableView = findViewById(R.id.rl_news_static);
        swipeRefreshLayout_ly = findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout_ly.setOnRefreshListener(this);
        refreshableView.setLayoutManager(new LinearLayoutManager(this));
        newsStaticAdapter = new NewsStaticAdapter(newsData,id,this);
        refreshableView.setAdapter(newsStaticAdapter);
        newsStaticAdapter.setOnLoadMoreListener(this);
        new Handler().postDelayed(() -> {
            swipeRefreshLayout_ly.setRefreshing(true);
            getNewMsg(1);

        }, 500);
        newsStaticAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                NewsBean.DataBean.ContentBean contentBean = newsData.get(position);
                Intent intent = new Intent(NewsStaticActivity.this, NewsDetailActivity.class);
                intent.putExtra("title",title);
                intent.putExtra("id",contentBean.getId());
                startActivity(intent);
            }
        });
    }
    public void getNewMsg(final int mCurrent) {
        HttpManager.getInstance().getNewsMsg(id, mCurrent, 10).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(this, new ApiSubscriber.HttpOnResultListener<NewsBean>() {
            @Override
            public void onNext(NewsBean newsBean) {
                if (mCurrent == 1) {
                    newsData.clear();
                    newsStaticAdapter.notifyDataSetChanged();
                }
                newsData.addAll(newsBean.getData().getContent());
                if (newsData.size() < newsBean.getData().getRecordTotal()) {
                    newsStaticAdapter.setEnableLoadMore(true);
                    current++;
                } else {
                    newsStaticAdapter.setEnableLoadMore(false);
                }
                newsStaticAdapter.notifyDataSetChanged();


            }

            @Override
            public void onCompleted() {
                swipeRefreshLayout_ly.setRefreshing(false);
                newsStaticAdapter.loadMoreComplete();

            }

            @Override
            public void onstart() {

            }
        }));
    }
    @Override
    public void onRefresh() {
        current = 1;
        getNewMsg(current);
    }


    @Override
    public void onLoadMoreRequested() {
        getNewMsg(current);
    }
}
